package com.qfedu.microuser.dailyReport.controller;

import com.qfedu.common.core.common.Result;
import com.qfedu.microuser.dailyReport.entity.DailyReport;
import com.qfedu.microuser.dailyReport.server.DailyReportService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("api/daily") // 接口前缀
public class DailyController {

    @Resource
    private DailyReportService dailyReportService;

    /**
     * 提交日报接口
     */
    @PostMapping("/submit")
    public Result submitDaily(@RequestBody DailyReport dailyReport) {
        // 简单参数校验
        if (dailyReport.getProjectName() == null || dailyReport.getProjectName().trim().isEmpty()) {
            return Result.error("项目名称不能为空");
        }
        if (dailyReport.getTodayWork() == null || dailyReport.getTodayWork().trim().isEmpty()) {
            return Result.error("今日工作不能为空");
        }
        if (dailyReport.getTomorrowWork() == null || dailyReport.getTomorrowWork().trim().isEmpty()) {
            return Result.error("明日工作不能为空");
        }

        boolean success = dailyReportService.submitDaily(dailyReport);
        return success
                ? Result.success("提交成功")
                : Result.error("提交失败，请重试");
    }


    /**
     * 查询日志列表（支持按日期筛选）
     * @param date 日期（格式：yyyy-MM-dd）
     * @return 日志列表
     */
    @GetMapping("/list")
    public List<DailyReport> getLogList(@RequestParam(required = false) String date) {
        LocalDate localDate = date != null ? LocalDate.parse(date) : null;
        return dailyReportService.getLogsByDate(localDate);
    }

}